# Copyright (C) 2011 Anders Bugge
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# Contact Info
# E-Mail: whimais@gmail.com

# Thanks To
# http://www.whiskeymedia.com/
# http://www.giantbomb.com/
# http://www.comicvine.com/
# http://www.screened.com/
# http://www.tested.com/
# http://www.animevice.com/

import wm

class VideoBase:
	site_id = -1
	latest_count = 100
	search_limit = 20
	cat_latest = 10000
	cat_search = 10001

	def __init__(self, site_id):
		self.site_id = site_id
	
	# Categories
	# Get online categories
	def getCategories(self):
		self.cats = wm.ListBase(self.site_id, "video_types", "field_list=id,name,deck")
		# Added latest, search and append all categories
		self.cat_list = []
		self.cat_list.append( {u'id': self.cat_latest, u'name': u'Latest', u'deck': u'The latest videos.'} )
		self.cat_list.append( {u'id': self.cat_search, u'name': u'Search', u'deck': u'Search for videos.'} )
		self.cat_list.extend( self.cats.getTotalList() )
		return self.cat_list
	
	# Videos
	# get videos depending on category
	def getVideos(self, category, query="", cb=None):
		# Latest
		if category == self.cat_latest:
			return self.getLatest()
		
		# Search
		elif category == self.cat_search:
			return self.search(query, cb)
		
		# Categories
		else:
			self.vids = wm.ListBase(self.site_id, "videos", "sort=publish_date&video_type="+str(category))
			return self.vids.getTotalList(cb=cb)
	
	# Latest
	# Return latest videos
	def getLatest(self):
		self.lat = wm.ListBase(self.site_id, "videos")
		if self.lat.getList(0, 0) == None:
			return None
		
		self.num = self.lat.getTotalSize()
		return self.lat.getList(self.num-self.latest_count, self.latest_count)
	
	# Search
	# query: search string
	def search(self, query, cb=None):
		self.vids = wm.ListBase(self.site_id, "search", "resources=video&query="+str(query.replace(" ", ",")))
		return self.vids.getTotalList(0, self.search_limit,cb=cb)
